home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk1 / kurta / kurta.c next >
C/C++ Source or Header  |  1995-03-18  |  8KB  |  327 lines

  1. /* kurta.c */
  2.  
  3. #include <exec/types.h>
  4. #include <exec/exec.h>
  5. #include <devices/serial.h>
  6. #include <devices/input.h>
  7. #include <devices/inputevent.h>
  8. #include <devices/timer.h>
  9. #include <stdio.h>
  10. #include <intuition/intuitionbase.h>
  11. #include <intuition/intuition.h>
  12. #include <functions.h>
  13.  
  14. static short done = 0;
  15. static struct IOExtSer *inreq = NULL;
  16. static unsigned char inbuf[513];
  17. static struct MsgPort *inputDevPort = NULL;
  18. static struct MsgPort *timerDevPort = NULL;
  19. static struct IOStdReq *input_request_block = NULL;
  20. static struct timerequest *tr = NULL;
  21. static short input_device_open = 0;
  22. static short timer_device_open = 0;
  23. static struct InputEvent motion_event;
  24. static struct InputEvent button_event;
  25.  
  26. struct IntuitionBase *IntuitionBase = NULL;
  27. struct GfxBase *GfxBase = NULL;
  28. struct Window *Window = NULL;
  29.  
  30. main()
  31. {
  32.     struct NewWindow NewWindow;
  33.     register struct IntuiMessage *msg;
  34.  
  35.     SetTaskPri(FindTask(0L), 20L);
  36.  
  37.     /* Open display */
  38.     if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L)))
  39.         CleanUp("no graphics library!!!");
  40.  
  41.     if (!(IntuitionBase = (struct IntuitionBase *)
  42.         OpenLibrary("intuition.library", 0L)))
  43.         CleanUp("no intuition here!!");
  44.  
  45.     NewWindow.LeftEdge = 250;
  46.     NewWindow.TopEdge = 0;
  47.     NewWindow.Width = 150;
  48.     NewWindow.Height = 10;
  49.     NewWindow.DetailPen = 0;
  50.     NewWindow.BlockPen = 1;
  51.     NewWindow.Title = (UBYTE *) "Kurta";
  52.     NewWindow.Flags = WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH
  53.         | SIMPLE_REFRESH;
  54.     NewWindow.IDCMPFlags = CLOSEWINDOW | REFRESHWINDOW;
  55.     NewWindow.FirstGadget = NULL;
  56.     NewWindow.CheckMark = NULL;
  57.     NewWindow.Type = WBENCHSCREEN;
  58.     NewWindow.Screen = NULL;
  59.     NewWindow.BitMap = NULL;
  60.     NewWindow.MinWidth = 150;
  61.     NewWindow.MinHeight= 10;
  62.     NewWindow.MaxWidth = 150;
  63.     NewWindow.MaxHeight= 10;
  64.  
  65.     if (!(Window = (struct Window *) OpenWindow(&NewWindow)))
  66.         CleanUp("Couldn't open the window.");
  67.  
  68.     /* prepare input device */
  69.     inputDevPort = CreatePort(0L, 0L);
  70.     if (inputDevPort == NULL)
  71.         CleanUp("Can't create port");
  72.     input_request_block = CreateStdIO(inputDevPort);
  73.     if (input_request_block == NULL)
  74.         CleanUp("Can't create input request block");
  75.     if (OpenDevice("input.device", NULL, input_request_block, NULL))
  76.         CleanUp("Can't open input device");
  77.     input_device_open = 1;
  78.  
  79.     input_request_block->io_Command = IND_WRITEEVENT;
  80.     input_request_block->io_Flags = 0;
  81.     input_request_block->io_Length = sizeof(struct InputEvent);
  82.  
  83.     motion_event.ie_NextEvent = NULL;
  84.     motion_event.ie_Class = IECLASS_POINTERPOS;
  85.     motion_event.ie_Code = IECODE_NOBUTTON;
  86.  
  87.     button_event.ie_NextEvent = NULL;
  88.     button_event.ie_Class = IECLASS_RAWMOUSE;
  89.     button_event.ie_X = 0;
  90.     button_event.ie_Y = 0;
  91.  
  92.     /* prepare timer device */
  93.     timerDevPort = CreatePort(0L, 0L);
  94.     if (timerDevPort == NULL)
  95.         CleanUp("Can't create port");
  96.     tr = AllocMem((long) sizeof(*tr), MEMF_CLEAR | MEMF_PUBLIC);
  97.     if (tr == NULL)
  98.         CleanUp("Can't create timer request");
  99.     tr->tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE;
  100.     tr->tr_node.io_Message.mn_Node.ln_Pri = 0;
  101.     tr->tr_node.io_Message.mn_ReplyPort = timerDevPort;
  102.     if (OpenDevice(TIMERNAME, UNIT_MICROHZ, tr, NULL))
  103.         CleanUp("Can't open timer device");
  104.     timer_device_open = 1;
  105.  
  106.     tr->tr_node.io_Command = TR_GETSYSTIME;
  107.  
  108.     /* prepare serial device */
  109.     inreq = (struct IOExtSer *)
  110.         AllocMem((long) sizeof(*inreq), MEMF_PUBLIC | MEMF_CLEAR);
  111.     if (inreq == NULL)
  112.         CleanUp("Can't allocate memory");
  113.     inreq->IOSer.io_Message.mn_ReplyPort = CreatePort("SerialRead", 0L);
  114.     if (inreq->IOSer.io_Message.mn_ReplyPort == NULL)
  115.         CleanUp("Can't create port");
  116.     inreq->io_SerFlags = SERF_XDISABLED;
  117.     if (OpenDevice(SERIALNAME, NULL, inreq, NULL))
  118.         CleanUp("Can't open Serial port");
  119.  
  120.     inreq->IOSer.io_Command = SDCMD_SETPARAMS;
  121.     inreq->io_ReadLen = inreq->io_WriteLen = 8;
  122.     inreq->io_Baud = 2400;
  123.     DoIO(inreq);
  124.  
  125.     inreq->IOSer.io_Command = CMD_READ;
  126.     inreq->IOSer.io_Data = (APTR) inbuf;
  127.     inreq->IOSer.io_Length = 1;
  128.     SendIO(inreq);
  129.  
  130.     while (!done) {
  131.         /* wait for something to do */
  132.         Wait(1L << inreq->IOSer.io_Message.mn_ReplyPort->mp_SigBit
  133.              | 1L << Window->UserPort->mp_SigBit);
  134.         if (CheckIO(inreq)) { /* received data from the tablet */
  135.             WaitIO(inreq);
  136.             Parse(inbuf, inreq->IOSer.io_Actual);
  137.             inreq->IOSer.io_Command = SDCMD_QUERY;
  138.             DoIO(inreq);
  139.             inreq->IOSer.io_Command = CMD_READ;
  140.             if (inreq->IOSer.io_Actual > 0) {
  141.                 inreq->IOSer.io_Length = inreq->IOSer.io_Actual;
  142.                 if (inreq->IOSer.io_Length > 512)
  143.                     inreq->IOSer.io_Length = 512;
  144.                 DoIO(inreq);
  145.                 Parse(inbuf, inreq->IOSer.io_Actual);
  146.                 inreq->IOSer.io_Length = 1;
  147.             }
  148.             SendIO(inreq);
  149.         }
  150.  
  151.         while (msg = (struct IntuiMessage *) GetMsg(Window->UserPort)) {
  152.             ReplyMsg(msg);
  153.             switch (msg->Class) {
  154.                 case CLOSEWINDOW: /* shut down */
  155.                     done = 1;
  156.                     break;
  157.                 default:
  158.                     break;
  159.             }
  160.         }
  161.     }
  162.  
  163.     CloseDevice(inreq);
  164.     CleanUp(NULL);
  165. }
  166.  
  167. /*
  168.  * Function to parse data tablet format
  169.  */
  170.  
  171. Parse(data, count)
  172. unsigned char *data;
  173. ULONG count;
  174. {
  175.     static int byte_index = 1;
  176.     static int x, y, switch2, switch3;
  177.  
  178.     while (count > 0) {
  179.         switch (byte_index) {
  180.             case 1:
  181.                 /* Try to sync up to tablet */
  182.                 if ((*data & 0x40) == 0)
  183.                     break;
  184. reset:
  185.                 switch2 = (*data & (0x10 | 0x08)) != 0;
  186.                 switch3 = (*data & 0x04) != 0;
  187.                 byte_index = 2;
  188.                 break;
  189.             case 2:
  190.                 if (*data & 0x40)
  191.                     goto reset;
  192.                 x = *data & 0x3F;
  193.                 byte_index = 3;
  194.                 break;
  195.             case 3:
  196.                 if (*data & 0x40)
  197.                     goto reset;
  198.                 x |= (*data & 0x1F) << 6;
  199.                 byte_index = 4;
  200.                 break;
  201.             case 4:
  202.                 if (*data & 0x40)
  203.                     goto reset;
  204.                 y = *data & 0x3F;
  205.                 byte_index = 5;
  206.                 break;
  207.             case 5:
  208.                 if (*data & 0x40)
  209.                     goto reset;
  210.                 y |= (*data & 0x1F) << 6;
  211.                 send_event(x, y, switch2, switch3);
  212.                 byte_index = 1;
  213.                 break;
  214.         }
  215.         count -= 1;
  216.         data += 1;
  217.     }
  218. }
  219.  
  220. send_event(x, y, switch2, switch3)
  221. int x, y, switch2, switch3;
  222. {
  223.     static int old_x = 0, old_y = 0;
  224.     static int old_switch2 = 0, old_switch3 = 0;
  225.  
  226.     if (x != old_x || y != old_y) {
  227.         input_request_block->io_Data = (APTR) &motion_event;
  228.         motion_event.ie_X = (2 * x) / 3 - 50;
  229.         if (motion_event.ie_X < 0)
  230.             motion_event.ie_X = 0;
  231.         else if (motion_event.ie_X > 640)
  232.             motion_event.ie_X = 640;
  233.         motion_event.ie_Y = 450 - (2 * y) / 3;
  234.         if (motion_event.ie_Y < 0)
  235.             motion_event.ie_Y = 0;
  236.         if (motion_event.ie_Y > 400)
  237.             motion_event.ie_Y = 400;
  238.         motion_event.ie_Qualifier = 
  239.             (old_switch3 ? IEQUALIFIER_LEFTBUTTON : 0) |
  240.             (old_switch2 ? IEQUALIFIER_RBUTTON : 0);
  241.  
  242.         /* get the current time */
  243.         DoIO(tr);
  244.         motion_event.ie_TimeStamp = tr->tr_time;
  245.  
  246.         DoIO(input_request_block);
  247.  
  248.         old_x = x;
  249.         old_y = y;
  250.     }
  251.  
  252.     if (switch3 != old_switch3) {
  253.         input_request_block->io_Data = (APTR) &button_event;
  254.         button_event.ie_Code = IECODE_LBUTTON;
  255.         if (!switch3)
  256.             button_event.ie_Code |= IECODE_UP_PREFIX;
  257.         button_event.ie_Qualifier = IEQUALIFIER_RELATIVEMOUSE |
  258.             (switch3 ? IEQUALIFIER_LEFTBUTTON : 0) |
  259.             (old_switch2 ? IEQUALIFIER_RBUTTON : 0);
  260.  
  261.         /* get the current time */
  262.         DoIO(tr);
  263.         button_event.ie_TimeStamp = tr->tr_time;
  264.  
  265.         DoIO(input_request_block);
  266.  
  267.         old_switch3 = switch3;
  268.     }
  269.  
  270.     if (switch2 != old_switch2) {
  271.         input_request_block->io_Data = (APTR) &button_event;
  272.         button_event.ie_Code = IECODE_RBUTTON;
  273.         if (!switch2)
  274.             button_event.ie_Code |= IECODE_UP_PREFIX;
  275.         button_event.ie_Qualifier = IEQUALIFIER_RELATIVEMOUSE |
  276.             (switch3 ? IEQUALIFIER_LEFTBUTTON : 0) |
  277.             (switch2 ? IEQUALIFIER_RBUTTON : 0);
  278.  
  279.         /* get the current time */
  280.         DoIO(tr);
  281.         button_event.ie_TimeStamp = tr->tr_time;
  282.  
  283.         DoIO(input_request_block);
  284.  
  285.         old_switch2 = switch2;
  286.     }
  287. }
  288.  
  289. /*
  290.  * Function to clean up however much stuff got started
  291.  */
  292.  
  293. static CleanUp(text)
  294. char *text;
  295. {
  296.     if (timer_device_open)
  297.         CloseDevice(tr);
  298.     if (tr)
  299.         FreeMem(tr, (long) sizeof(*tr));
  300.     if (timerDevPort)
  301.         DeletePort(timerDevPort);
  302.  
  303.     if (input_device_open)
  304.         CloseDevice(input_request_block);
  305.     if (input_request_block)
  306.         DeleteStdIO(input_request_block);
  307.     if (inputDevPort)
  308.         DeletePort(inputDevPort);
  309.  
  310.     if (inreq) {
  311.         if (inreq->IOSer.io_Message.mn_ReplyPort)
  312.             DeletePort(inreq->IOSer.io_Message.mn_ReplyPort);
  313.  
  314.         FreeMem(inreq, (long) sizeof(*inreq));
  315.     }
  316.  
  317.     if (Window) CloseWindow(Window);
  318.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  319.     if (GfxBase) CloseLibrary(GfxBase);
  320.  
  321.     if (text) {
  322.         fprintf(stderr, "ERROR: %s\n", text);
  323.         exit(100);
  324.     }
  325.     exit(FALSE);
  326. }
  327.